namespace ActiveRecord

import System
import System.Collections
import System.Collections.Generic
import System.Text

#encapsulates a collection of ActiveRecordBase
#class ActiveTable(IEnumerable of ActiveRecordBase, ICollection):
class ActiveTable(IEnumerable, ICollection):
   // properties
   Length as int:
      get:
         if _rows != null: 
            return _rows.Count
         else: 
            return 0
   [Property(Name)]
   _name as string
   _rows = List of ActiveRecordBase()
   _textField as string
   _valueField as string
   // public methods
   def constructor():
      pass

   def constructor(ary as List of ActiveRecordBase):
      _rows = ary

   def constructor(count as int):
      _rows = List of ActiveRecordBase(count)

   def constructor( table as ActiveTable, textField as string, valueField as string ):
      _rows = table._rows
      _textField = textField
      _valueField = valueField
   
   def constructor(name as string):
      _name = name

   def Add(activeRecord as ActiveRecordBase):
      _rows.Add(activeRecord)
   
   def Add(activeTable as ActiveTable):
      for activeRec as ActiveRecordBase in activeTable:
         Add(activeRec)
   
   def GetValue(index as int) as ActiveRecordBase:
      if(index < _rows.Count):
         return _rows[index] as ActiveRecordBase
      else:
         return null

   public override def ToString() as string:
      sb = StringBuilder()
      for ar as ActiveRecordBase in _rows:
         sb.Append(ar)
      return sb.ToString()

   public DataItem:
      get:
         return _rows

/*
# waiting for http://jira.codehaus.org/browse/BOO-965
#region IEnumerable<ActiveRecordBase> members
   public def IEnumerable[of ActiveRecordBase].GetEnumerator() as IEnumerator of ActiveRecordBase:
      return _rows.GetEnumerator()
#endregion 
*/

#region IEnumerable members
   public def GetEnumerator() as System.Collections.IEnumerator:
      return _rows.GetEnumerator()
#endregion

#region ICollection members
   public def CopyTo(ary as Array, idx as int):
      i = 0
      for idx as int in range(idx, _rows.Count):
         ary.SetValue(_rows[idx], i++)

   public def CopyTo(ary as Array):
      CopyTo(ary, 0)

   IsSynchronized as bool:
      get:
         return false

   SyncRoot as object:
      get:
         return null

   Count as int:
      get:
         return _rows.Count

#endregion

   self[index as int] as ActiveRecordBase:
      get:
         return GetValue(index)
/*
#region IDataSource members
   
   protected def GetViewNames() as ICollection:
      al = ArrayList(1)
      al.Add("DefaultView")
      return al as ICollection

   protected def GetView(name as string) as DataSourceView:
      #return SqlDataSourceView(self)
      return null as DataSourceView

   event DataSourceChanged as EventHandler

#endregion
*/
